package com.example.myblog.controller;

import com.example.myblog.dto.UserDTO;
import com.example.myblog.exception.BaseException;
import com.example.myblog.result.BaseCodeEnum;
import com.example.myblog.result.ResponseBean;
import com.example.myblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 叶兴
 */
@RestController
public class LoginController {


    UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     * @throws BaseException
     */
    @GetMapping("/login")
    public ResponseBean login(@RequestParam("username") String username,
                              @RequestParam("password") String password) throws BaseException{
        String token = userService.selectUser(username, password);
        return ResponseBean.success(token);

    }

    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     * @throws BaseException
     */
    @PostMapping("/register/{username}/{password}")
    public ResponseBean register(@PathVariable String username,
                                 @PathVariable String password) throws BaseException {
        userService.insertUser(username, password);
        return ResponseBean.success();
    }

    /**
     * 用户退出
     * @param username
     * @return
     * @throws BaseException
     */
    @GetMapping("logout/{username}")
    public ResponseBean logout(@PathVariable String username) throws BaseException{
        userService.logout(username);
        return ResponseBean.success();
    }


}
